package com.woniuxy.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.pojo.CoachVo;
import com.woniuxy.yoga.pojo.Message;
import com.woniuxy.yoga.pojo.MessageVo;
import com.woniuxy.yoga.service.MessageService;
import com.woniuxy.yoga.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author axq
 * @since 2020-10-29
 */
@RestController
@CrossOrigin
@RequestMapping("/yoga/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    //聘请教练方法
    @ApiOperation(value = "发生招聘信息")
    @GetMapping(value = "/saveMessage/{fromid}/{toid}/{username}")
    public R saveMessage(@PathVariable("fromid") Integer fromid, @PathVariable("toid") Integer toid, @PathVariable("username") String username,Message message){
        System.out.println(fromid);
        System.out.println(toid);
        System.out.println(username);
        message.setFromid(fromid);
        message.setToid(toid);
        message.setUsername(username);
        boolean age = messageService.save(message);

        if (age) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }

    //俱乐部查看聘请信息
    @ApiOperation(value = "俱乐部查看聘请信息")
    @GetMapping(value = "/club2message")
    @CrossOrigin
    public R getCoachByPage(@ApiParam(value = "当前页")@RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
                            @ApiParam(value = "每页的记录数")@RequestParam(value = "limit",required = true,defaultValue = "6") Integer limit,
                            MessageVo messageVo){
        //创建Page对象
        Page pageBean = new Page<>(page,limit);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(messageVo.getUsername())){
            queryWrapper.like("username",messageVo.getUsername());
        }
        if(messageVo.getFromid()!=null){
            queryWrapper.eq("fromid",messageVo.getFromid());
        }
        Page resultPage = messageService.page(pageBean, queryWrapper);

        //获取集合
        List records = resultPage.getRecords();

        //获取总记录数
        long total = resultPage.getTotal();
        if(records!=null){
            return R.ok().data("records",records).data("total",total);
        }else{
            return R.error().message("没有查到集合数据");
        }

    }

}

